package MyLinkeList;

public class MyLinkeList<T> implements IList{
    class ListNode
    {
        public T val;
        public ListNode next;
        public ListNode prev;

        public ListNode(T val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(Object data) {
        ListNode node =new ListNode((T) data);
        if(head == null)
        {
            head = node;
            last = node;
        }
        else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(Object data) {
        ListNode node = new ListNode((T)data);
        if(head == null)
        {
            head = node;
            last = node;
        }
        else
        {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    private void addIndexException(int index) throws myLinkeListException
    {
        if(index < 0 || index > this.size())
        {
            throw new myLinkeListException("插入下标异常");
        }
    }

    private void findIndexException(int index) throws myLinkeListException
    {
        if(index < 0 || index > this.size())
        {
            throw new myLinkeListException("寻找下标异常");
        }
    }

    private ListNode findIndex (int index)
    {
        this.findIndexException(index);
        ListNode cur = this.head;
        int count = 0;
        while(count != index)
        {
            cur = cur.next;
            count++;
        }

        return cur;
    }
    @Override
    public void addIndex(int index, Object data) {
        addIndexException(index);

        if(index == 0)
        {
            this.addFirst(data);
            return;
        }
        else if(index == this.size())
        {
            this.addLast(data);
            return;
        }

        ListNode cur = this.findIndex(index);
        ListNode node = new ListNode((T)data);
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }

    @Override
    public boolean contains(Object key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.val.equals(key))
            {
                return true;
            }
        }
        return false;
    }

    @Override
    public void remove(Object key) {
        ListNode cur = head;
        while(cur != null)
        {
            if(cur.val.equals(key))
            {
                if(cur == head)
                {
                    head = head.next;
                    if(head == null)
                    {
                        last = null;
                    }
                    else {
                        head.prev = null;
                    }
                }
                else
                {
                    cur.prev.next = cur.next;
                    if(cur.next == null)
                    {
                        last = last.prev;
                    }
                    else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            else
            {
                cur = cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(Object key) {
        ListNode cur = head;
        while(cur != null)
        {
            if(cur.val.equals(key))
            {
                if(cur == head)
                {
                    head = head.next;
                    if(head == null)
                    {
                        last = null;
                    }
                    else
                    {
                        head.prev = null;
                    }
                }
                else {
                    cur.prev.next = cur.next;
                    if(cur.next == null)
                    {
                        last = last.prev;
                    }
                    else
                    {
                        cur.next.prev = cur.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        head = null;
        last = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
